import xml.etree.ElementTree as ET  
from xml.etree.ElementTree import SubElement

from ArxmlBasicTools.Arxml import arxmlFile
from ArxmlBasicTools.SwcAutoConfig import SwcComponentConfig


class CompositionConfig(object):

    path = 'no define'
    name = 'no define'
    element = 'no define'
    componentHeader = 'no define'
    componentDict = dict()
    connectorsHeader = 'no define'
    connectorsDict = dict()

    def __init__(self,path,name) -> None:
        self.path = path
        self.name = name
        self.element = ET.Element("COMPOSITION-SW-COMPONENT-TYPE")
        SubElement(self.element,'SHORT-NAME').text = name  

    def createComposition(self,path,name):
        self.path = path
        self.name = name
        self.element = ET.Element("COMPOSITION-SW-COMPONENT-TYPE")
        SubElement(self.element,'SHORT-NAME').text = name  

    def addComponent(self,Component: SwcComponentConfig,createCompFlag:bool = True):
        self.componentDict['CPT_' + Component.name] = Component

        if createCompFlag == True:
            if self.componentHeader == 'no define':
                self.componentHeader = SubElement(self.element,'COMPONENTS')
            
            swc_el = SubElement(self.componentHeader,'SW-COMPONENT-PROTOTYPE')
            SubElement(swc_el,'SHORT-NAME').text = 'CPT_' + Component.name
            SubElement(swc_el,'TYPE-TREF', attrib={'DEST': 'APPLICATION-SW-COMPONENT-TYPE'}).text = Component.path + '/' + Component.name

    def addConnectors(self,PPortComponentName,PPortName,RPortComponentName,RPortName):
        if self.connectorsHeader == 'no define':
            self.connectorsHeader = SubElement(self.element,'CONNECTORS')
        connenctor_el = SubElement(self.connectorsHeader,'ASSEMBLY-SW-CONNECTOR')
        SubElement(connenctor_el,'SHORT-NAME').text = 'ASC_CPT_' + PPortComponentName + '_' + PPortName + '_CPT_' + RPortComponentName + '_' + RPortName
        PPortRef_el = SubElement(connenctor_el,'PROVIDER-IREF')
        SubElement(PPortRef_el,'CONTEXT-COMPONENT-REF', attrib={'DEST': 'SW-COMPONENT-PROTOTYPE'}).text = self.path + '/' + self.name + '/CPT_' + PPortComponentName
        SubElement(PPortRef_el,'TARGET-P-PORT-REF', attrib={'DEST': 'P-PORT-PROTOTYPE'}).text = self.componentDict['CPT_' + PPortComponentName].path + '/' + PPortComponentName + '/' + PPortName
        RPortRef_el = SubElement(connenctor_el,'REQUESTER-IREF')
        SubElement(RPortRef_el,'CONTEXT-COMPONENT-REF', attrib={'DEST': 'SW-COMPONENT-PROTOTYPE'}).text = self.path + '/' + self.name + '/CPT_' + RPortComponentName
        SubElement(RPortRef_el,'TARGET-R-PORT-REF', attrib={'DEST': 'R-PORT-PROTOTYPE'}).text = self.componentDict['CPT_' + RPortComponentName].path + '/' + RPortComponentName + '/' + RPortName
        
class SystemArxmlFile(arxmlFile):
    def __init__(self) -> None:
        super(SystemArxmlFile, self).__init__()
        self.element = 'no define'
        self.SystemPath = 'no define'

    def createSystemArxmlFile(self,PackagePath):
        self.SystemPath = PackagePath
        self.element = self.createArxmlPackage(PackagePath)

    def addComposistion(self,composition:CompositionConfig):
        self.element.append(composition.element)

